from five import grok
from zope import schema
from plone.directives import form

from zope.interface import Invalid

from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm

from plone.namedfile.interfaces import IImageScaleTraversable
from plone.namedfile.field import NamedBlobImage

from plone.app.textfield import RichText

from plumvillage.registration import PlumVillageMessageFactory as _

# Indexer
from plone.indexer import indexer

# Uniqueness validator
from z3c.form import validator
from plone.uuid.interfaces import IUUID
from Products.CMFCore.utils import getToolByName

# Room list
from zope.schema.interfaces import IContextSourceBinder
from plumvillage.registration.room import IRoom

@grok.provider(IContextSourceBinder)
def availableRooms(context):
    """ Return a catalog search result of available rooms
    """

    # Get a list of available rooms for room assignment
    catalog = getToolByName(context, 'portal_catalog')
    results = catalog({'object_provides': IRoom.__identifier__,
                       'sort_on' : 'sortable_title'})
    terms = []

    if results is not None:
        for room in results:
            terms.append(SimpleVocabulary.createTerm(room.getId, room.Title))
    return SimpleVocabulary(terms)

class IReservation(form.Schema):
    """Describes a reservation for one person over a determined period
    """

    startDate = schema.Date(
            title=_(u'Start date'),
            description=_(u"First date that the film is showing"),
        )
        
    endDate = schema.Date(
            title=_(u'End date'),
            description=_(u"Last date that the film is showing"),
            required=True
        )

    birthdate = schema.Date(
            title=_(u"Birthdate"),
            description=_(u"Please enter the person's date of birth"),
            required=True
        )


    gender = schema.Choice(
            title=_(u"Gender"),
            values=(u"Male", u"Female",)
        )

    room = schema.Choice(
            title=_(u"Room assignment"),
            source=availableRooms,
            required=False
         )


@grok.adapter(IReservation, name='start')
@indexer(IReservation)
def reservationStartIndexer(context):
    """Create a catalogue indexer, registered as an adapter, which can
    populate the ``start`` index with the reservation's start date.
    """
    return context.startDate

@grok.adapter(IReservation, name='end')
@indexer(IReservation)
def reservationEndIndexer(context):
    """Create a catalogue indexer, registered as an adapter, which can
    populate the ``end`` index with the reservation's end date.
    """
    return context.endDate

class View(grok.View):
    """Default view (called "@@view"") for a reservation.
    
    The associated template is found in reservation_templates/view.pt.
    """
    
    grok.context(IReservation)
    grok.require('zope2.View')
    grok.name('view')
    
    def update(self):
        """Prepare information for the template
        """
        
        self.startDateFormatted = self.context.startDate.strftime("%d %b %Y")
        self.endDateFormatted = self.context.endDate.strftime("%d %b %Y")
